/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testapplication.sprite;

/**
 *
 * @author Fabio
 */
import java.lang.reflect.Constructor;

import rawengine.gamecore.GameCore;
import rawengine.graphics.Texture;
import rawengine.graphics.animation.Animation;
import rawengine.graphics.animation.AnimationState;
import rawengine.math.Vector2f;


/**
    The Player.
*/
public class Player extends Creature {

    private static final float JUMP_SPEED = -.65f;

    private boolean onGround;
    private Animation stand;
    private Animation jump;
   

   
    public Player(String name, Texture image, Vector2f boundsCollision)
    {
        super(name, image, boundsCollision);
    	
    }

 
    @Override
    public void collideHorizontal() 
    {
        setVelocityX(0);
    }


    @Override
    public void collideVertical() 
    {
        // check if collided with ground
        if (getVelocityY() > 0) 
        {
            onGround = true;
            
        }
        setVelocityY(0);
    }


    @Override
    public void setY(float y) 
    {
        // check if falling
        if (Math.round(y) > Math.round(getY())) {
            onGround = false;
        }
        super.setY(y);
    }


    @Override
    public void wakeUp() 
    {
        // do nothing
    }
    
    public void setAnimations()
    {
    	super.setAnimations();
    	setStand();
    	setJump();
    }
    
	    /**
	    Updates the animaton for this creature.
	*/
	public void update() 
	{
	    // select the correct Animation
				
	    	    
		if (getVelocityX() > 0) {
			walk.setState(AnimationState.NORMAL);
        	newAnim = walk;
        }
        else if (getVelocityX() < 0) {
        	walk.setState(AnimationState.MIRROR);
            newAnim = walk;
        }
        
        if (getState() == STATE_DYING && newAnim.getState() == AnimationState.NORMAL) {
        	dead.setState(AnimationState.FLIPPED);
            newAnim = dead;
        }
        else if (getState() == STATE_DYING && newAnim.getState() == AnimationState.MIRROR) {
        	dead.setState(AnimationState.MIRROR_FLIPPED);
            newAnim = dead;
        }
	    if (getVelocityX() == 0 && newAnim.getState() == AnimationState.MIRROR) {
	    	stand.setState(AnimationState.MIRROR);
	        newAnim = stand;
	    }
	    else if(getVelocityX() == 0 && newAnim.getState() == AnimationState.NORMAL)
	    {
	    	stand.setState(AnimationState.NORMAL);
	    	newAnim = stand;
	    }
	   	    
	    if (onGround == false && newAnim.getState() == AnimationState.MIRROR) {
	    	jump.setState(AnimationState.MIRROR);
	        newAnim = jump;
	    }
	    else if (onGround == false && newAnim.getState() == AnimationState.NORMAL) {
	    	jump.setState(AnimationState.NORMAL);
	        newAnim = jump;
	    }

	    // update the Animation
        if (getCurrentAnimation() != newAnim) 
        {
        	setCurrentAnimation(newAnim);
            getCurrentAnimation().start();
        }
        else {
        	
            getCurrentAnimation().update();
        }

        // update to "dead" state
        stateTime += GameCore.timeHandler.getFrameTime();
        if (getState() == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
	}


    /**
        Makes the player jump if the player is on the ground or
        if forceJump is true.
    */
    public void jump(boolean forceJump) {
        if (onGround || forceJump) {
            onGround = false;
            setVelocityY(JUMP_SPEED);
            
        }
    }


    @Override
    public float getMaxSpeed() {
        return .4f;
    }
    
    @SuppressWarnings("rawtypes")
	@Override
    public Object clone() {
        // use reflection to create the correct subclass
        Constructor constructor = getClass().getConstructors()[0];
        try {
            return constructor.newInstance(new Object[] {
                 getName(),
                 getImage(),
                 boundsCollision});
        }
        catch (Exception ex) {
            // should never happen
            ex.printStackTrace();
            return null;
        }
    }


	public Animation getStand() 
	{
		return stand;
	}


	public void setStand()
	{
		stand = getAnimation("Stand");
	}


	public Animation getJump() 
	{
		return jump;
	}


	public void setJump() 
	{
		jump = getAnimation("jump");
		
	}

}